package com.imageim.yimirror.net;

import android.content.Context;

import com.imageim.yimirror.common.network.NetUtil;
import com.imageim.yimirror.common.network.listener.NetCallback;
import com.imageim.yimirror.common.network.tool.ParamCreator;

import org.apache.http.message.BasicNameValuePair;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Yuaihen.
 * on 2018/11/29
 * 网络实现类
 */
public class NetPresenterImpl {

    private static final String TAG = "NetPresenterImpl";
    private NetView mNetView;

    public NetPresenterImpl(NetView netView) {
        mNetView = netView;
    }

    /**
     * Get方式请求
     *
     * @param url      api接口地址 api/getShopConfigInfo
     * @param paramMap 传递给后台的参数集合
     * @param msgCode  消息请求码,用来区分不同的网络请求
     * @param <T>      json解析后生成的对象Class
     */
    public <T> void getObject(Context context, String url, HashMap<String, String> paramMap, Class<T> clazz, int msgCode) {
        ParamCreator params = new ParamCreator();
        if (paramMap != null) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                params.add(entry.getKey(), entry.getValue());
            }
        }

        List<BasicNameValuePair> build = params.build();
        NetUtil.getObject(context, url, build, new NetCallback<T>() {
            @Override
            public void onStart() {
                if (mNetView != null) {
                    mNetView.onLoadPre(msgCode);
                }
            }

            @Override
            public void onSuccess(T result) {
                if (mNetView != null) {
                    mNetView.onLoadSuccess(result, msgCode);
                }
            }


            @Override
            public void onFail(int errorCode, String errorMsg) {
                if (mNetView != null) {
                    mNetView.onLoadFail(errorCode, errorMsg, msgCode);
                }
            }
        }, clazz);

    }


    /**
     * Get方式请求  return 字符串 非json或bean对象
     *
     * @param url      api接口地址 api/getShopConfigInfo
     * @param paramMap 传递给后台的参数集合
     * @param msgCode  消息请求码,用来区分不同的网络请求
     */
    public void getObject(Context context, String url, HashMap<String, String> paramMap, int msgCode) {
        ParamCreator params = new ParamCreator();
        if (paramMap != null) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                params.add(entry.getKey(), entry.getValue());
            }
        }

        List<BasicNameValuePair> build = params.build();
        NetUtil.getObject(context, url, build, new NetCallback<String>() {
            @Override
            public void onStart() {
                if (mNetView != null) {
                    mNetView.onLoadPre(msgCode);
                }
            }

            @Override
            public void onSuccess(String result) {
                if (mNetView != null) {
                    mNetView.onLoadSuccess(result, msgCode);
                }
            }

            @Override
            public void onFail(int errorCode, String errorMsg) {
                if (mNetView != null) {
                    mNetView.onLoadFail(errorCode, errorMsg, msgCode);
                }
            }
        });

    }


    /**
     * Post方式请求 返回bean
     *
     * @param url      api接口地址 api/getShopConfigInfo
     * @param paramMap 传递给后台的参数集合
     * @param msgCode  消息请求码,用来区分不同的网络请求
     * @param <T>      json解析后生成的对象Class
     */
    public <T> void postObject(Context context, String url, HashMap<String, String> paramMap, Class<T> clazz, int msgCode) {
        ParamCreator params = new ParamCreator();
        if (paramMap != null) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                params.add(entry.getKey(), entry.getValue());
            }
        }

        List<BasicNameValuePair> build = params.build();
        NetUtil.postObject(context, url, build, new NetCallback<T>() {
            @Override
            public void onStart() {
                if (mNetView != null) {
                    mNetView.onLoadPre(msgCode);
                }
            }

            @Override
            public void onSuccess(T result) {
                if (mNetView != null) {
                    mNetView.onLoadSuccess(result, msgCode);
                }
            }


            @Override
            public void onFail(int errorCode, String errorMsg) {
                if (mNetView != null) {
                    mNetView.onLoadFail(errorCode, errorMsg, msgCode);
                }
            }
        }, clazz);

    }


    /**
     * Post方式请求 返回String
     *
     * @param url      api接口地址 api/getShopConfigInfo
     * @param paramMap 传递给后台的参数集合
     * @param msgCode  消息请求码,用来区分不同的网络请求
     */
    public void postObject(Context context, String url, HashMap<String, String> paramMap, int msgCode) {
        ParamCreator params = new ParamCreator();
        if (paramMap != null) {
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                params.add(entry.getKey(), entry.getValue());
            }
        }

        List<BasicNameValuePair> build = params.build();
        NetUtil.postObject(context, url, build, new NetCallback<String>() {
            @Override
            public void onStart() {
                if (mNetView != null) {
                    mNetView.onLoadPre(msgCode);
                }
            }

            @Override
            public void onSuccess(String result) {
                if (mNetView != null) {
                    mNetView.onLoadSuccess(result, msgCode);
                }
            }


            @Override
            public void onFail(int errorCode, String errorMsg) {
                if (mNetView != null) {
                    mNetView.onLoadFail(errorCode, errorMsg, msgCode);
                }
            }
        });

    }


    /**
     * 销毁之后重置引用View
     */
    public void onDestroy() {
        mNetView = null;
    }

}

